<?php

//ini_set("display_errors",1);

//require_once 'PHPExcel-1.8/Classes/PHPExcel.php';


ini_set("display_errors",1);

include 'PHPExcel-1.8/Classes/PHPExcel.php';
include 'PHPExcel-1.8/Classes/PHPExcel/IOFactory.php';

define('EXCEL_EXTENSION_2003', "xls");
define('EXCEL_EXTENSION_2007', "xlsx");
$fileName2007 = "123.xlsx";
$fileName = $fileName2007;
$reader = new PHPExcel_Reader_Excel2007();
$PHPExcel = $reader->load($fileName);
$worksheet = $PHPExcel->getActiveSheet();
$highestRow = $worksheet->getHighestRow(); // 取得总行数
$highestColumm = $worksheet->getHighestColumn(); // 取得总列数
$highestColumm= PHPExcel_Cell::columnIndexFromString($highestColumm); //字母列转换为数字列 如:AA变为27

$imageInfo = extractImageFromWorksheet($worksheet,"images/");
/** 循环读取每个单元格的数据 */
$result = array();
for ($row = 2; $row <= $highestRow; $row++) {//行数是以第1行开始
    $item = array();
    $item['create_time'] = time();
    $item['pic'] = $imageInfo[$row - 2];
    for ($column = 1; $column < $highestColumm; $column++) {//列数是以第0列开始
        switch ($column){
            case 1:{
                $item['name']=$worksheet->getCellByColumnAndRow($column, $row)->getValue();
                break;
            }
            case 2:{
                $item['type']=$worksheet->getCellByColumnAndRow($column, $row)->getValue();
                break;
            }
            case 3:{
                $item['registration']=$worksheet->getCellByColumnAndRow($column, $row)->getValue();
                break;
            }
            case 4:{
                $item['approved']=$worksheet->getCellByColumnAndRow($column, $row)->getValue();
                break;
            }
            case 5:{
                $item['cluster']=$worksheet->getCellByColumnAndRow($column, $row)->getValue();
                break;
            }
            case 6:{
                $item['price']=$worksheet->getCellByColumnAndRow($column, $row)->getValue();
                break;
            }
            case 7:{
                $item['phone']=$worksheet->getCellByColumnAndRow($column, $row)->getValue();
                break;
            }
            case 8:{
                $item['shows']=$worksheet->getCellByColumnAndRow($column, $row)->getValue();
                break;
            }
            case 9:{
                $item['whetherown']=$worksheet->getCellByColumnAndRow($column, $row)->getValue();
                break;
            }
        }
    }
    array_push($result,$item);
}
//
print_r($result);
function getExtendFileName($file_name) {

    $extend = pathinfo($file_name);
    $extend = strtolower($extend["extension"]);
    return $extend;
}

function extractImageFromWorksheet($worksheet,$basePath){

    $result = array();
    $imageFileName = "";

    foreach ($worksheet->getDrawingCollection() as $drawing) {
        $path = $basePath;
            $filename = $drawing->getPath();
            $suiji=rand(1000,9999);
            $imageFileName = time().'_'.$suiji.$drawing->getIndexedFilename();
            $path = $path .time().'_'.$suiji.$drawing->getIndexedFilename();
            copy($filename, $path);
        array_push($result,$path);

    }
    return $result;
}